package com.guigu.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.erp.pojo.DesignConflgFileKind;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.pojo.DesignModule;
import com.guigu.erp.service.DesignConflgFileKindService;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.service.DesignModuleService;
import com.guigu.erp.utils.NumberUtils;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignFileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * ClassName:DesignFileController
 * Package:com.guigu.erp.controller
 * Description:
 *
 * @Date:2024/2/21 18:50
 * @Author:chenchong
 */
@CrossOrigin
@Controller
@RequestMapping("/design")
public class DesignFileController {

    @Autowired
    private DesignFileService designFileService;
    @Autowired
    private DesignModuleService designModuleService;

    @Autowired
    private NumberUtils numberUtils;
    // 产品档案查询请求
    @RequestMapping("/designFileList")
    @ResponseBody
    public PageUtils<DesignFile> designFileList(DesignFileVo vo, int pageNo, int pageSize) {
        PageUtils<DesignFile> page = designFileService.page(vo, pageSize, pageNo);
        return page;
    }
//    // 产品档案查询请求
//    @RequestMapping("/designFileList1")
//    @ResponseBody
//    public PageUtils<DesignFile> designFileList1(@RequestBody  DesignFileVo vo, int pageNo, int pageSize) {
//        PageUtils<DesignFile> page = designFileService.page1(vo, pageSize, pageNo);
//        return page;
//    }

    // 产品档案查询请求
    @RequestMapping("/designFileList1/{currentPage}/{pageSize}")
    @ResponseBody
    public PageUtils<DesignFile> designFileList1(@RequestBody  DesignFileVo vo,
                                                @PathVariable int currentPage,
                                                 @PathVariable  int pageSize) {
        PageUtils<DesignFile> page = designFileService.page1(vo,  pageSize, currentPage);
        return page;
    }
    // 产品档案查询请求
    @RequestMapping("/designFileList2/{currentPage}/{pageSize}")
    @ResponseBody
    public PageUtils<DesignFile> designFileList2(@RequestBody DesignFileVo vo, @PathVariable int currentPage,@PathVariable int pageSize) {
        PageUtils<DesignFile> page = designFileService.page2(vo, pageSize, currentPage);
        return page;
    }
//产品档案添加
    @RequestMapping("/add")
    @ResponseBody
    public boolean add(DesignFile designFile) {
        String firstKindName = designFile.getFirstKindName();
        String[] split = firstKindName.split("/");
        designFile.setFirstKindId(split[1]);
        designFile.setFirstKindName(split[2]);


        String secondKindName = designFile.getSecondKindName();
        String[] secondAry = secondKindName.split("/");
        designFile.setSecondKindId(secondAry[1]);
        designFile.setSecondKindName(secondAry[2]);

        String thirdKindName = designFile.getThirdKindName();
        String[] thirdAry = thirdKindName.split("/");
        designFile.setThirdKindId(thirdAry[1]);
        designFile.setThirdKindName(thirdAry[2]);

        // 设置标志位属性
        // 审核标志: 等待审核
        designFile.setCheckTag("S001-0");
        // 档案变更标志: 未变更
        designFile.setChangeTag("D002-0");
        // 价格变更标志：未变更
        designFile.setPriceChangeTag("J001-0");
        // 产品删除标志: 未删除
        designFile.setDeleteTag("C001-0");
        // 物料组成标志: 未设计
        designFile.setDesignModuleTag("W001-0");
        // 工序组成标志: 未设计
        designFile.setDesignProcedureTag("G001-0");
        // 库存分配标志: 未设计
        designFile.setDesignCellTag("K001-0");

//        // 建档时间
//        designFile.setRegisterTime(new Date());

        // 生成对应的产品档案编号
        String productNumber = numberUtils.getProductNumber(designFile.getFirstKindId(),
                designFile.getSecondKindId(),
                designFile.getThirdKindId());
        // 设置产品编号
        designFile.setProductId(productNumber);

        // 插入
        boolean flag = designFileService.save(designFile);
        if(flag) {
            return flag;
        }
        return flag;

    }

    //显示未审核数据
    @RequestMapping("/designReview")
    @ResponseBody
    public  PageUtils<DesignFile>  designReview(int pageNo, int pageSize){

        PageUtils<DesignFile> list = designFileService.designReview1(pageNo,pageSize);
        return list;
    }

    //根据id查询产品档案信息
    @RequestMapping("/designReviewId")
    @ResponseBody
    public DesignFile designReviewId(int id){
        QueryWrapper<DesignFile> qw=new QueryWrapper<>();
        qw.eq("ID",id);
        DesignFile list = designFileService.getOne(qw);
        return list;
    }
    //产品档案复核
    @RequestMapping("/update")
    @ResponseBody
    public Boolean update(DesignFile designFile){
        String firstKindName = designFile.getFirstKindName();
        String[] split = firstKindName.split("/");
        designFile.setFirstKindId(split[0]);
        designFile.setFirstKindName(split[1]);


        String secondKindName = designFile.getSecondKindName();
        String[] secondAry = secondKindName.split("/");
        designFile.setSecondKindId(secondAry[0]);
        designFile.setSecondKindName(secondAry[1]);

        String thirdKindName = designFile.getThirdKindName();
        String[] thirdAry = thirdKindName.split("/");
        designFile.setThirdKindId(thirdAry[0]);
        designFile.setThirdKindName(thirdAry[1]);

        designFile.setCheckTag("S001-1");
        boolean b = designFileService.updateById(designFile);
        return b;

    }
    // 查询未制定过物料设计单的产品档案
    @RequestMapping("/designFileUnMakeList")
    @ResponseBody
    public PageUtils<DesignFile> designFileUnMakeList(DesignFileVo vo,
                                                      int pageNo,
                                                      int pageSize) {
        PageUtils<DesignFile> page = designFileService.unmakePage(vo, pageNo, pageSize);
        return page;
    }

    //根据产品档案id查询对应的档案信息
    @RequestMapping("/file")
    @ResponseBody
    public DesignFile fileOne(int id){
        DesignFile byId = designFileService.getById(id);
        return byId;
    }

    //根据产品档案id查询对应的档案信息
    @RequestMapping("/file/moduleFileList")
    @ResponseBody
    public PageUtils<DesignFile> moduleFileList(DesignFileVo vo,int pageNo, int pageSize){

        PageUtils<DesignFile> designFilePageUtils = designFileService.moduleFileList(vo, pageNo, pageSize);
        return designFilePageUtils;
    }
    //根据产品档案id查询对应的档案信息
    @RequestMapping("/file/moduleFileList2")
    @ResponseBody
    public PageUtils<DesignFile> moduleFileList2(DesignFileVo vo,int pageNo, int pageSize){

        PageUtils<DesignFile> designFilePageUtils = designFileService.moduleFileList2(vo, pageNo, pageSize);
        return designFilePageUtils;
    }
    // 查询未制定过工序设计单的产品档案
    @RequestMapping("/designFileUnMakeList1")
    @ResponseBody
    public PageUtils<DesignFile> designFileUnMakeList1(DesignFileVo vo,
                                                      int pageNo,
                                                      int pageSize) {
        PageUtils<DesignFile> page = designFileService.unmakePage1(vo, pageNo, pageSize);
        return page;
    }

    @RequestMapping("/productIdshow")
    @ResponseBody
    public DesignFile productIdshow(String  productId){
        QueryWrapper<DesignFile> qw=new  QueryWrapper<DesignFile>();
        qw.eq("PRODUCT_ID",productId);
        DesignFile one = designFileService.getOne(qw);
        return one;
    }

    @RequestMapping("/productIdshow2")
    @ResponseBody
    public Boolean productIdshow2(String  productId){
        QueryWrapper<DesignFile> qw=new  QueryWrapper<>();
        qw.eq("PRODUCT_ID",productId);
        DesignFile one = designFileService.getOne(qw);
        return !one.getDeleteTag().equals("C001-1");
    }
}
